package structure.proxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class ProxyFactory<T> {


    public T getProxy(Class<T> cls) throws InstantiationException, IllegalAccessException {
        T object = cls.newInstance();
        Object o = Proxy.newProxyInstance(object.getClass().getClassLoader(),
                object.getClass().getInterfaces(),
                (proxy, method, args) -> {
                    System.out.println("proxy is beginning");
                    Object result = method.invoke(object, args);
                    System.out.println("proxy is ending");
                    return result;

                });
        return (T) o;
    }
}
